/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.virtual.model;

import offset.nodes.Constants;
import offset.nodes.client.model.TreeNodeUserObject;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.PropertyDefinition;


public class SchemaNode extends SchemaItem implements TreeNodeUserObject, Cloneable {
    private NodeTypeDefinition type;
    private NodeDefinition node;
    
    public SchemaNode(QName name, NodeDefinition node, NodeTypeDefinition type) {
        super((QName)name.clone());
        if (type != null)
            this.type = (NodeTypeDefinition) type.clone();
        else
            this.type = null;
        if ( node != null)
            setNode((NodeDefinition)node.clone());
        else
            setNode(null);
    }
    
    public NodeTypeDefinition getNodeType() {
        return type;
    }
    
    public void setType(NodeTypeDefinition type) {
        this.type = type;
    }
    public NodeDefinition getNode() {
        return node;
    }
    
    public void setNode(NodeDefinition node) { 
        this.node = node;
    }
    
    public static SchemaNode newNode() {
        NodeTypeDefinition type = new NodeTypeDefinition();
        type.setName(new QName(Constants.JCR_NT_BASE, ""));
        SchemaNode definition = new SchemaNode(new QName(Constants.URI_NODES_DATA, ""), new NodeDefinition(), type );
        
        return definition;
    }
    
    public static SchemaNode newType() {
        NodeTypeDefinition type = new NodeTypeDefinition();
        type.setPropertyDefs(new PropertyDefinition[0]);
        type.setChildNodeDefs(new NodeDefinition[0]);
        type.setName(new QName(Constants.URI_NODES_DATA, ""));
        SchemaNode definition = new SchemaNode(type.getName(), null, type );
        
        return definition;
    }

    public String getType() {
        return SchemaItem.TYPE_NODE;
    }
    
}
